package org.lc.stk.web.dto.department;

import java.util.List;
import java.util.stream.Collectors;

import org.lc.stk.model.baseInfo.Department;
import org.lc.stk.model.baseInfo.DepartmentType;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

@Data
@Schema(description = "部门响应DTO")
public class DepartmentResponse {

    @Schema(description = "部门ID")
    private Integer id;

    @Schema(description = "部门名称")
    private String name;

    @Schema(description = "部门编码")
    private String code;

    @Schema(description = "部门类型ID")
    private Integer typeId;

    @Schema(description = "部门类型名称")
    private String typeName;

    @Schema(description = "上级部门ID")
    private Integer parentId;

    @Schema(description = "上级部门名称")
    private String parentName;

    @Schema(description = "排序号")
    private Integer orderNum;

    @Schema(description = "备注")
    private String remark;

    @Schema(description = "子部门列表")
    private List<DepartmentResponse> children;

    public static DepartmentResponse fromEntity(Department department) {
        if (department == null) {
            return null;
        }
        
        DepartmentResponse response = new DepartmentResponse();
        response.setId(department.getId());
        response.setName(department.getName());
        response.setCode(department.getCode());
        response.setOrderNum(department.getOrderNum());
        response.setRemark(department.getRemark());

        if (department.getType() != null) {
            response.setTypeId(department.getType().getId());
            response.setTypeName(department.getType().getName());
        }

        if (department.getParent() != null) {
            response.setParentId(department.getParent().getId());
            response.setParentName(department.getParent().getName());
        }

        if (department.getChildren() != null) {
            response.setChildren(
                department.getChildren().stream()
                    .map(DepartmentResponse::fromEntity)
                    .collect(Collectors.toList())
            );
        }

        return response;
    }
}